/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.dispositivos;

import com.mapping.Marca;
import com.opensymphony.xwork2.ActionSupport;
import static com.opensymphony.xwork2.Action.SUCCESS;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Andres
 */
@Namespace(value="/")
@Action(value="buscarMarca", results={@Result(name="success",location="/GestionDispositivos/rBuscarMarca.jsp")})
public class BuscarMarcaAction extends ActionSupport{
    private String tipo;
    private List<Marca> listaMarcas;
    
    @Override
    public String execute(){
        String consulta = "select distinct m.IDMARCA,m.MARCA from marca m inner join modelo mo on (m.IdMarca=mo.IdMarca) "
                + "inner join tipo_disp td on (mo.idTipo=td.idTipo) where td.idTipo=?1";
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em =  emf.createEntityManager();  
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        Query query =  em.createNativeQuery(consulta,Marca.class);
        query.setParameter(1,tipo);
        listaMarcas = (List<Marca>) query.getResultList();
        tx.commit();
        em.close();
        return SUCCESS;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    public List<Marca> getListaMarcas() {
       return listaMarcas;
    }
    
    
}
